package m202401.d03;

import java.util.ArrayList;
import java.util.List;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/3 09:08
 */
//228. 汇总区间
public class Q228 {
    public List<String> summaryRanges(int[] nums) {
        List<String> rs = new ArrayList<>();
        for (int i = 0; i < nums.length; ) {
            int next = findNext(nums, i);
            if (next == i) {
                rs.add(String.valueOf(nums[i]));
                i++;
            } else {
                rs.add(nums[i] + "->" + nums[next]);
                i = next + 1;
            }
        }
        return rs;
    }

    private int findNext(int[] nums, int index) {
        int start = nums[index];
        for (int i = index + 1; i < nums.length; i++) {
            if (nums[i] == start + 1) {
                start++;
            } else {
                return i - 1;
            }
        }
        return nums.length - 1;
    }
}
